<chapter xml:id="utf8.reader.h">
<title><tt>__vic/utf8/reader.h</tt></title>

<chapter xml:id="utf8--reader">
<title><tt>utf8::reader</tt></title>

<code-block lang="C++"><![CDATA[
template<class ByteSReader>
class utf8::reader
{
public:
    using byte_reader_type = ByteSReader;
    ByteSReader &get_byte_reader();
    const ByteSReader &get_byte_reader() const;

    template<class... Args>
    explicit reader(Args&&... args); // since C++11

    reader(); // C++98 only
    explicit reader(ByteSReader r); // C++98 only

    utf8::read_result parse();
    sread_result<unicode_t> read()
    sread_result<unicode_t> operator()();
};

template<class ByteSReader>
utf8::reader<ByteSReader> utf8::make_reader(ByteSReader r);
]]></code-block>

<p>UTF-8 code points from byte sequence reader. The sequence is accessed using
<tt>ByteSReader</tt> which models <tt>sreader&lt;unsigned char></tt> (see
<xref to="sreaders"/>).</p>

<section><title>Class members</title>

<synopsis>
<prototype>ByteSReader &amp;get_byte_reader()</prototype>
<prototype>const ByteSReader &amp;get_byte_reader() const</prototype>
<p>Returns reference to the used byte reader.</p>
</synopsis>

<synopsis>
<prototype>template&lt;class... Args>
explicit reader(Args&amp;&amp;... args) <badge>C++11</badge></prototype>
<p>Forwards all parameters to the used byte reader.</p>
</synopsis>

<synopsis>
<prototype>reader() <badge>C++98 only</badge></prototype>
<prototype>explicit reader(ByteSReader r) <badge>C++98 only</badge></prototype>
<p>Constructors for C++98 mode.</p>
</synopsis>

<synopsis>
<prototype>utf8::read_result parse()</prototype>
<p>Tries to extract the next code point from the byte sequence using
<tt>ByteSReader</tt>. Returns the code point read on success.
<tt>utf8::status::eof</tt> is returned when no more bytes are available.
Other statuses are returned on errors, see <xref to="utf8--status"/> for more
details. <tt>ByteSReader::operator()</tt> is used to access individual bytes.</p>
<note>The function itself doesn't throw exceptions but exception can be
thrown by <tt>ByteSReader::operator()</tt>.</note>
</synopsis>

<synopsis>
<prototype>sread_result&lt;unicode_t> read()</prototype>
<prototype>sread_result&lt;unicode_t> operator()()</prototype>
<p>The same as <tt>parse()</tt> but returns <tt>sread_result</tt> on success or
on EOF. Throws an exception from <xref to="utf8.exceptions.h"/> otherwise.</p>
</synopsis>

</section>

<section><title>Free functions</title>

<synopsis>
<prototype>template&lt;class ByteSReader>
utf8::reader&lt;ByteSReader> utf8::make_reader(ByteSReader r)</prototype>
<p>Creates UTF-8 reader using the specified <tt>ByteSReader</tt>.</p>
</synopsis>

</section>

<section><title>Example</title>
<code-block lang="C++"><![CDATA[
#include<__vic/utf8/reader.h>
#include<__vic/sreaders/string.h>
#include<string>
#include<cstdint>
#include<iostream>

// C++11
using utf8_string_reader = __vic::utf8::reader<__vic::string_sreader>;

// C++98
struct utf8_string_reader : __vic::utf8::reader<__vic::string_sreader>
{
    explicit utf8_string_reader(const std::string &s)
        : __vic::utf8::reader<__vic::string_sreader>(__vic::string_sreader(s)) {}
};

void print_utf8_code_points(const string &s)
{
    utf8_string_reader r(s);
    while(__VIC_SREAD_RESULT(unicode_t) cp = r())
        std::cout << uint_least32_t(cp.value()) << '\n';
}
]]></code-block>
</section>

</chapter>

</chapter>
